import {FetchRequest} from 'fe-lib';
import store from 'store';

const {
  Consts,
  createAPI,
  connectToRedux,
  resetResponseStatic
} = FetchRequest;

connectToRedux(store);

// if the schema of response is not like
// { headers: { responseCode: xxx, responseMessage: xxx }, body: { ... } }
// you need to reset the response like below
resetResponseStatic({
  // 200 <= code < 300
  isSuccess: status => status === 0 || status === '0',
  parseHeaders: (res) => {
    const responseCode = res.code;
    const responseMessage = res.message;
    if (responseCode === 401 && res.content) {
      console.log("not login ,redirect to login url")
      window.location.href = res.content || res.content;
    } else if (responseCode === 403) {
      // gotoUnAuthorized();
    }
    return {responseCode, responseMessage};
  },
  parseBody: res => res.result
});

export {
  Consts,
  createAPI
};
